﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Libero.FusionCharts;
using Libero.FusionCharts.Template;
using Libero.FusionCharts.Abstract;
using System.Threading;

namespace WebApplication1
{
    public partial class Exemplo_Ajax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["chartType"] != null)
            {
                string xmlData = GetSalesChart(
                                                Request["period"].ToString(),
                                                Request["chartType"].ToString(),
                                                Request["chartTemplate"].ToString()
                                            ).ToXML();

                //loaded by ajax
                Response.Clear();
                Response.ClearContent();
                Response.ContentType = "text/xml";
                Response.Write(xmlData);
                Response.End();
            }
            else
            {
                //default.
                chtSales.ShowChart(GetSalesChart("2010", "Area2DChart", "OfficeTemplate"));
            }
        }

        public SingleSeriesChart GetSalesChart(string period, string chartType, string chartTemplate)
        {
            SingleSeriesChart oChart;

            switch (chartType)
            {
                case "Area2DChart": oChart = new Area2DChart(); break;
                case "Bar2DChart": oChart = new Bar2DChart(); break;
                case "Column2DChart": oChart = new Column2DChart(); break;
                case "Column3DChart": oChart = new Column3DChart(); break;
                case "LineChart": oChart = new LineChart(); break;
                default: throw new Exception("chart type not expected.");
            }

            switch (chartTemplate)
            {
                case "OfficeTemplate": oChart.Template = new OfficeTemplate(); break;
                case "OfficeLightTemplate": oChart.Template = new OfficeLightTemplate(); break;
                case "OfficeDarkTemplate": oChart.Template = new OfficeDarkTemplate(); break;
                case "OceanTemplate": oChart.Template = new OceanTemplate(); break;
                default: throw new Exception("chart template not expected.");
            }

            oChart.ChartTitles.Caption = "Anual Sales";
            oChart.ChartTitles.SubCaption = "Period 2010";

            oChart.DataSource = GetData(period).ToList();
            oChart.DataTextField = "Month";
            oChart.DataValueField = "Total";

            return oChart;
        }

        private IEnumerable<Sales> GetData(string period)
        {
            int p = int.Parse(period); // sort a random number by period 
            yield return new Sales { Month = "Jan", Total = p == 2010 ? 21 : 25 };
            yield return new Sales { Month = "Feb", Total = p == 2009 ? 24 : 21 };
            yield return new Sales { Month = "Mar", Total = p == 2008 ? 25 : 24 };
            yield return new Sales { Month = "Apr", Total = p == 2010 ? 24 : 25 };
            yield return new Sales { Month = "Mai", Total = p == 2009 ? 21 : 20 };
            yield return new Sales { Month = "Jun", Total = p == 2010 ? 20 : 21 };
            yield return new Sales { Month = "Jul", Total = p == 2009 ? 19 : 23 };
            yield return new Sales { Month = "Aug", Total = p == 2008 ? 22 : 24 };
            yield return new Sales { Month = "Sep", Total = p == 2009 ? 25 : 25 };
            yield return new Sales { Month = "Oct", Total = p == 2010 ? 25 : 22 };
            yield return new Sales { Month = "Nov", Total = p == 2008 ? 24 : 20 };
            yield return new Sales { Month = "Dec", Total = p == 2009 ? 20 : 30 };
        }

        private class Sales
        {
            public string Month { get; set; }
            public decimal Total { get; set; }
        }
    }
}